package cn.kgb.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.UUID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

import cn.kgb.dao.UpFileDAO;
import cn.kgb.javabean.UpLoadFile;
import cn.kgb.javabean.User;

public class UpFileServlet extends HttpServlet{
	private   ServletContext sc=null;
	 public void init() throws ServletException {
		 super.init();
		 sc=this.getServletContext();
	 }
	 
	 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		    doPostValidate(request, response);
	 }
	 
	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		 doPostValidate(request, response);
	 }
	 private void doBrowse(HttpServletRequest request, HttpServletResponse response,String url) throws
	    ServletException, IOException{
	    RequestDispatcher rd=sc.getRequestDispatcher(url);
	    rd.forward(request,response); 
	  }
	 
	 public void doPostValidate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		 boolean ret = true;
		 User user = (User)request.getSession().getAttribute("user");
		 String dir = request.getParameter("dir");
		 String realDir = dir.replaceAll("/", "\\\\\\\\");
		 System.out.println("realDir:"+realDir);
			String fname = UUID.randomUUID().toString();
			DiskFileUpload upload = new DiskFileUpload();
		  Iterator it = null;
		  String files = null;
		try {
			it = upload.parseRequest(request).iterator();
		} catch (FileUploadException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		  while(it.hasNext()){
		  	FileItem fileitem=(FileItem)it.next();
		  	files = new String(fileitem.getName());
		  	files = files.substring(files.lastIndexOf("\\") + 1);

		  	if(!fileitem.isFormField()){
					try{
						
						fileitem.write(new File(request.getRealPath("/")+"upfile\\"+user.getId()+realDir+files));
					}catch(Exception e){
						ret = false;
						System.out.print(e);
						
					}
		  	}
		  	break;
		  }
		  
		  if(ret){
			  UpFileDAO upfileDAO = new UpFileDAO();
			  UpLoadFile file = new UpLoadFile();
			  file.setUserid(user.getId());
			  file.setType(1);
			  file.setDir(dir);
			  file.setFilename(files);
			  file.setInfo("no");
			  String ddate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
			  file.setDate(ddate);
			  
			  upfileDAO.InsertFile(file);
			  request.getSession().setAttribute("user", user);
			  this.doBrowse(request, response, "/content/files/userfile.jsp");
		  }
	 }
}
